# encoding: UTF-8
"""

@author = 李林名
@email = lishuai1199@qq.com
@create_time = 2022/11/24 11:11

"""


from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep


# #1.打印点击网页后,网页的标题:   白月黑羽测试网页3
# wd = webdriver.Chrome()
# wd.get('https://cdn2.byhy.net/files/selenium/sample3.html')
# ##根据属性选择元素
# link = wd.find_element(By.CSS_SELECTOR, 'a')
# link.click()
# ##打印网页的标题
# print(wd.title)
# sleep(1)


# #2.进入页面内的其他页面，并加载新的窗口:   打开必应,并在搜索框中输入"试试就试试",点击搜索
# wd = webdriver.Chrome()
# wd.get('https://cdn2.byhy.net/files/selenium/sample3.html')
# ##根据属性选择元素
# link = wd.find_element(By.CSS_SELECTOR, 'a')
# link.click()
# ##进入bing窗口
# for handle in wd.window_handles:
#     wd.switch_to.window(handle)
#     if '必应' in wd.title:
#         break
#
# ##获取搜索框的outerHTML数据
# element=wd.find_element(By.CSS_SELECTOR,'#sb_form_q')
# element.send_keys('试试就试试')
# # element=wd.find_element(By.CSS_SELECTOR,'#sb_form> #sb_form_go')
# # element.click()
# print(element.get_attribute('outerHTML'))
# sleep(2)


# 3.进入bing窗口,返回原来窗口:   打开必应,返回原来窗口,并点击id为outerbutton的按钮
wd = webdriver.Chrome()
wd.get('https://cdn2.byhy.net/files/selenium/sample3.html')
##根据属性选择元素
link = wd.find_element(By.CSS_SELECTOR, 'a')
link.click()

##保存当前窗口句柄
mainWindow=wd.current_window_handle
##进入bing窗口句柄
for handle in wd.window_handles:
    wd.switch_to.window(handle)
    if '必应' in wd.title:
        break
sleep(2)
##返回主窗口
wd.switch_to.window(mainWindow)
##获取主窗口id为outerbuttion元素,并点击
element=wd.find_element(By.CSS_SELECTOR,'#outerbutton')
sleep(1)
element.click()
sleep(1)
element.click()
sleep(2)